home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / tls / tls074c.sunsparc.Z / tls074c.sunsparc / lib / vtcl / examples / Browser.tcl < prev    next >
Encoding:
Text File  |  1995-07-20  |  5.5 KB  |  203 lines

  1. #!/bin/vtcl
  2. # ---------------------------------------------------------------------
  3. # Copyright 1994 by SCO, Inc.
  4. # Permission to use, copy, modify, distribute, and sell this software
  5. # and its documentation for any purpose is hereby granted without fee,
  6. # provided that the above copyright notice appear in all copies and that
  7. # both that copyright  notice  and  this  permission  notice  appear  in
  8. # supporting documentation, and that the name  of  SCO  not  be used  in
  9. # advertising or publicity pertaining  to distribution of  the  software
  10. # without   specific,   written  prior   permission.    SCO   makes   no
  11. # representations  about  the  suitability  of  this  software  for  any
  12. # purpose.  It is provided "as is" without express or implied warranty.
  13. # ---------------------------------------------------------------------
  14.  
  15. # Demo        : Browser.tcl
  16. # Description    : Demo Launcher
  17. # Comments    : This demo lists and fires up other Visual Tcl demos
  18. #          located in /lib/vtcl/examples.
  19. # Things to 
  20. # Look for    - reading in files via -filename option
  21. #        - checking for file readability with "file" command.
  22. #         - displaying error dialog
  23. #         - exec'ing commands (in background)
  24. #         - using catch for handling errors
  25. #         - how to retrieve a dialog's built-in buttons (ok, cancel...)
  26. #        - 
  27.  
  28. proc closeCB { cbs } {
  29.     VtClose; exit
  30. }
  31.  
  32. set currentFileName ""
  33.  
  34. # This callback (associated with the list widget) will retrieve
  35. # the name of the filename from the list and call the procedure
  36. # to display the file's contents in the text widget.
  37.  
  38. proc GetFileCB { cbs } {
  39.     global currentFileName listW
  40.     set dlog [keylget cbs dialog]
  41.     set newFileName [keylget cbs selectedItemList]
  42.  
  43.     # This demo wants to keep at least one item highlighted all the time.
  44.     # So, if user de-selects, we'll force it to be selected again.
  45.     if {$newFileName == ""} {
  46.         VtListSelectItem $listW -item $currentFileName
  47.         return
  48.     } else {
  49.         set currentFileName $newFileName
  50.     }
  51.  
  52.  
  53.     DisplayFile $newFileName $dlog
  54. }
  55.  
  56. # DisplayFile opens the filename for reading and updates the label at
  57. # the main dialog with the current filename and updates the text widget
  58. # with the contents of the file.
  59.  
  60. proc DisplayFile {filename dialog} {
  61.     global textW executePushB sourceLabel
  62.  
  63.     VtSetValues $sourceLabel \
  64.         -label "Source for \"$filename\""
  65.  
  66.     if { [file readable $filename] } {
  67.         VtSetValues $textW \
  68.             -filename $filename
  69.         VtSetSensitive $executePushB True
  70.         VtSetFocus $executePushB
  71.     } else {
  72.         VtSetValues $textW \
  73.             -value " < Unable to read your file > "
  74.     }
  75. }
  76.  
  77. # This procedure displays a little status about the program
  78. # that's getting exec'd.  This provides an example of hiding
  79. # an existing widget (or form, in this case) since you know 
  80. # it's likely to be used over and over.  The very cool Vtcl
  81. # command "info" is used to check for the existence of the
  82. # variable containing the formdialog's name.  It's a global
  83. # variable, so if it doesn't exist, you know that the form
  84. # hasn't been created yet.  If it does exist, then you use
  85. # VtSetValues to update the label in it and call VtShow.
  86. #
  87. # This is _real_ useful if you have a complex dialog that 
  88. # tends to take awhile to re-build before it gets displayed.
  89. #
  90. proc DisplayExecInfo {filename parent flag} {
  91.     global infoD executePushB
  92.     if {$flag == "On"} {
  93.         VtLock
  94.         if { ! [info exists infoD]} {
  95.             set infoD [VtFormDialog $parent.infoD \
  96.                 -title "Browser Status" \
  97.                 -wmDecoration {TITLE BORDER} \
  98.                 -resizable True \
  99.                 ]
  100.             set lbl [VtLabel $infoD.lbl \
  101.                 -label "    Exec'ing ${filename}...    " \
  102.                 -font medItalicFont \
  103.                 -rightSide FORM -bottomSide FORM \
  104.                 ]
  105.         } else {
  106.             VtSetValues $infoD.lbl -label "Exec'ing ${filename}..."
  107.         }
  108.         VtShow $infoD
  109.     } else {
  110.         VtHide $infoD
  111.         VtUnLock
  112.         VtSetFocus $executePushB
  113.     }
  114. }
  115.  
  116. # Fire up the currently displayed/selected script.
  117. #
  118. proc execCB { cbs } {
  119.     global currentFileName
  120.     set filename $currentFileName
  121.     set parent [keylget cbs dialog]
  122.     DisplayExecInfo $filename $parent On
  123.     set returnCode [catch {exec vtcl $filename &} errorMsg ]
  124.  
  125.     # using the combination of ; and #, you can put a comment on
  126.     # the same line as the command:
  127.     #
  128.     sleep 2 ; # seconds
  129.     DisplayExecInfo $filename $parent Off
  130.  
  131.     if {$returnCode != 0} {
  132.         set msg "Can't execute \"$filename\", error was:\n$errorMsg"
  133.         set errD [VtErrorDialog $parent.errorBox \
  134.             -ok \
  135.             -message $msg \
  136.             ]
  137.         VtShow $errD
  138.     }
  139.  
  140. }
  141.  
  142. #
  143. # Open connection with Visual Tcl display engine
  144. #
  145. set app [VtOpen browser]
  146.  
  147. # Note that rather than get an "Ok" or "Apply" button, we're
  148. # going to re-label the button names.  
  149. #
  150. set form [VtFormDialog $app.Top\
  151.     -title "Demo: Visual Tcl Browser"\
  152.     -applyLabel "Execute..." \
  153.     -applyCallback execCB \
  154.     -okLabel "Quit" \
  155.     -okCallback closeCB \
  156.     ]
  157.  
  158. # Make the execute button insensitive; retrieve the widget name 
  159. # of the main form's apply button.
  160. #
  161. set executePushB [VtGetValues $form -apply]
  162. VtSetSensitive $executePushB False
  163.  
  164. set llabel [VtLabel $form.llabel\
  165.     -topSide FORM \
  166.     -leftSide FORM \
  167.     -label "Vtcl Scripts" \
  168.     ]
  169.  
  170. # retrieve the list of files in the current directory
  171. set files [lsort [glob *.tcl]]
  172.  
  173. set listW [VtList $form.listW \
  174.     -selection SINGLE \
  175.     -MOTIF_topOffset 2 \
  176.     -bottomSide FORM \
  177.     -itemList $files \
  178.     -defaultCallback execCB \
  179.     -callback GetFileCB \
  180.     ]
  181.  
  182. set sourceLabel [VtLabel $form.sourceLabel \
  183.     -topSide FORM \
  184.     -leftSide $listW \
  185.     -rightSide FORM \
  186.     -label "Source" \
  187.     ]
  188.  
  189. set textW [VtText $form.textW \
  190.     -leftSide $listW \
  191.     -rightSide FORM \
  192.     -bottomSide FORM \
  193.     -topSide $sourceLabel \
  194.     -rows 20 \
  195.     -columns 70 \
  196.     -horizontalScrollBar TRUE \
  197.     -verticalScrollBar TRUE \
  198.     -readOnly \
  199.     ]
  200.  
  201. VtShow $form
  202. VtMainLoop
  203.